package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequirePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author L5775
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    // 处理查询请求
    // localhost/employee/list.do
    // JSP 查询的数据存到 Model 对象
    @RequestMapping("/list")
    @RequirePermission(name = "员工页面",expression = "employee:list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        PageResult<Employee> pageResult = employeeService.query(qo);
        model.addAttribute("pageResult", pageResult);
        model.addAttribute("departments",departmentService.listAll());
        //model.addAttribute("qo",qo);
        // /WEB-INF/views/employee/list.jsp
        return "employee/list";
    }

    // 处理删除请求
    // http://localhost/employee/delete.do?id=1
    // 删除完了还希望浏览器再发送请求访问 localhost/employee/list.do
    @RequestMapping("/delete")
    @ResponseBody
    @RequirePermission(name="员工删除",expression = "employee:delete")
    public JsonResult delete(Long id) {
        try {
            if (id != null) {
                employeeService.delete(id);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"系统异常,请联系管理员");
        }
    }

    // 处理去保存或者去修改请求
    // http://localhost/employee/input.do
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        //查询所有的角色
        model.addAttribute("roles",roleService.listAll());
        //查询所有部门数据 Model 中
        model.addAttribute("departments",departmentService.listAll());
        if (id != null) {
            Employee employee = employeeService.get(id);
            model.addAttribute("employee",employee);
        }
        return "employee/input";
    }

    // 处理保存或修改请求
    // http://localhost/employee/saveOrUpdate.do 并带了保存员工数据
    // 保存完了还希望浏览器再发送请求访问 localhost/employee/list.do
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Employee employee,Long[] ids){
        if (employee.getId() == null) {
            employeeService.save(employee,ids);
        }else {
            employeeService.update(employee, ids);
        }
        return "redirect:/employee/list.do";
    }
}
